package com.collectplus.express.parcel.fragment;

import android.content.Intent;
import android.os.Message;
import android.support.v4.widget.SwipeRefreshLayout;

import com.collectplus.express.R;
import com.collectplus.express.logic.AppHttp;
import com.collectplus.express.logic.AppResult;
import com.collectplus.express.logic.JSON;
import com.collectplus.express.logic.MsgID;
import com.collectplus.express.model.ParcelDetailBeanNew;
import com.collectplus.express.model.ParcelReceiveWayAndSignedBean;
import com.collectplus.express.parcel.NewParcelDetailActivity;
import com.collectplus.express.parcel.ParcelReceiveWayAdapter;

/**
 * 在途中的包裹
 *
 * @author kaka<br>
 *         2017-1-10上午11:01:49
 */
public class ParcelReceiveNoPayFragment extends BaseReceiverParcelFragment {

    private ParcelReceiveWayAdapter mAdapter;

    private boolean isRequest = true;
    private String parcelId;

    @Override
    protected void findViewById() {
        setContentView(R.layout.parcel_receive_list);
        super.findViewById();
        mAdapter = new ParcelReceiveWayAdapter(null, this);
        mListView.setAdapter(mAdapter);

        mRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                AppHttp.getInstance().getReceiverWayParcelList();
            }
        });
    }

    @Override
    public boolean handleMessage(Message msg) {
        switch (msg.what) {
            case MsgID.receiver_parcel_way_list:
                onRefreshComplete();
                cancelLoadingDialog();
                final AppResult<ParcelReceiveWayAndSignedBean> result = JSON.parseList(msg.obj, ParcelReceiveWayAndSignedBean.class);
                if (result.isSuccess()) {
                    getActivityContext().runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            if (mAdapter == null) {
                                mAdapter = new ParcelReceiveWayAdapter(result.getResultList(), ParcelReceiveNoPayFragment.this);
                                mListView.setAdapter(mAdapter);
                            } else {
                                mAdapter.setItems(result.getResultList(), true);
                            }
                        }
                    });
                } else {
                    showToast(result.getMessage());
                }
                return true;
            //获取包裹详情
            case MsgID.parcel_detail:
                cancelLoadingBar();
                final AppResult<ParcelDetailBeanNew> appResult = JSON.parser(msg.obj, ParcelDetailBeanNew.class);
                if (appResult.isSuccess()) {
                    ParcelDetailBeanNew bean = appResult.getResult();
                    Intent intent = new Intent(getActivityContext(), NewParcelDetailActivity.class);
                    intent.putExtra("parcelId", parcelId);
                    intent.putExtra("item", bean);
                    startActivity(intent);
                } else {
                    showToast(appResult.getMessage());
                }
                return false;
            default:
                return super.handleMessage(msg);
        }
    }

    public void getParcelDetail(String parcelId) {
        this.parcelId = parcelId;
        showLoadingDialog(null);
        AppHttp.getInstance().getParcelDetailNew(parcelId, 0);
    }

    @Override
    public void onStop() {
        super.onStop();
        isRequest = true;
    }

    @Override
    public void onResume() {
        if (isRequest) {
            showLoadingDialog(null);
            AppHttp.getInstance().getReceiverWayParcelList();
        }
        isRequest = false;
        super.onResume();
    }
}
